/**
 * 
 */
package edu.hawaii.halealohacli.command;

import static org.junit.Assert.assertEquals;
import java.util.Calendar;
import java.util.Locale;
import javax.xml.datatype.XMLGregorianCalendar;
import org.junit.Test;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Test class for DailyEnergy.
 * 
 * @author Sergey Negrashov
 * 
 */
public class TestDailyEnergy {

  /**
   * Url for the wattdepot server.
   */
  private String url = "http://server.wattdepot.org:8190/wattdepot/";

  /**
   * Argument array for the command.
   */
  private String args[] = { "Mokihana", "2011-11-23" };

  /**
   * Test to make sure that the method runs with valid inputs.
   */
  @Test
  public void testDailyEnergyValidData() {
    DailyEnergy command = new DailyEnergy();
    WattDepotClient client = new WattDepotClient(url);
    command.initialize(args, client);
    boolean ok = true;
    try {
      assertEquals("DailyEnergy test with valid data failed",
          command.printDailyEnergy().length() > 0, true);
    }
    catch (NotAuthorizedException e) {
      ok = false;
    }
    catch (ResourceNotFoundException e) {
      ok = false;
    }
    catch (BadXmlException e) {
      ok = false;
    }
    catch (MiscClientException e) {
      ok = false;
    }
    command.execute();
    assertEquals("DailyEnergy test with valid data failed", true, ok);
  }

  /**
   * Test to make sure that the method through an exception if the source name is invalid.
   */
  @Test
  public void testDailyEnergyBadSource() {
    args[0] = "BadSource";
    DailyEnergy command = new DailyEnergy();
    WattDepotClient client = new WattDepotClient(url);
    command.initialize(args, client);
    boolean ok = false;
    try {
      command.printDailyEnergy();
    }
    catch (NotAuthorizedException e) {
      ok = false;
    }
    catch (ResourceNotFoundException e) {
      ok = true;
    }
    catch (BadXmlException e) {
      ok = false;
    }
    catch (MiscClientException e) {
      ok = false;
    }
    assertEquals("DailyEnergy test with an invalid source", true, ok);
  }

  /**
   * Test to make sure that method does not return anything if the date argument is invalid.
   * 
   * @throws NotAuthorizedException should not throw and exception if the date is invalid.
   * @throws ResourceNotFoundException should not throw and exception if the date is invalid.
   * @throws BadXmlException should not throw and exception if the date is invalid.
   * @throws MiscClientException should not throw and exception if the date is invalid.
   */
  @Test
  public void testDailyEnergyBadTime() throws NotAuthorizedException, ResourceNotFoundException,
      BadXmlException, MiscClientException {
    args[0] = "Ilima";
    args[1] = "Bad-Date";
    DailyEnergy command = new DailyEnergy();
    WattDepotClient client = new WattDepotClient(url);
    command.initialize(args, client);
    assertEquals("DailyEnergy test with a bad date", command.printDailyEnergy(), "");
  }

  /**
   * Test to make sure that method does not return anything if the date argument is in the future.
   * 
   * @throws NotAuthorizedException should not throw and exception if the date is invalid.
   * @throws ResourceNotFoundException should not throw and exception if the date is invalid.
   * @throws BadXmlException should not throw and exception if the date is invalid.
   * @throws MiscClientException should not throw and exception if the date is invalid.
   */
  @Test
  public void testDailyEnergyFutureTime() throws NotAuthorizedException, ResourceNotFoundException,
      BadXmlException, MiscClientException {
    args[1] = "2034-11-13";
    DailyEnergy command = new DailyEnergy();
    WattDepotClient client = new WattDepotClient(url);
    command.initialize(args, client);
    assertEquals("DailyEnergy test with a bad date", command.printDailyEnergy(), "");
  }

  /**
   * Test to make sure that method returns a result when a current date is presented as an argument.
   * 
   * @throws NotAuthorizedException should not throw and exception if the date is invalid.
   * @throws ResourceNotFoundException should not throw and exception if the date is invalid.
   * @throws BadXmlException should not throw and exception if the date is invalid.
   * @throws MiscClientException should not throw and exception if the date is invalid.
   */
  @Test
  public void testDailyEnergyCurrentTime() throws NotAuthorizedException,
      ResourceNotFoundException, BadXmlException, MiscClientException {
    Calendar cal = Calendar.getInstance(Locale.US);
    XMLGregorianCalendar today = Tstamp.makeTimestamp(cal.getTimeInMillis());
    today = Tstamp.incrementMinutes(today, -60);
    args[1] = today.toString();
    DailyEnergy command = new DailyEnergy();
    WattDepotClient client = new WattDepotClient(url);
    command.initialize(args, client);
    assertEquals("DailyEnergy test with current time failed",
        command.printDailyEnergy().length() > 0, true);
  }
}
